package com.zl.zlbaseproject.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Created by 郑磊 on 2018/3/12.
 */

public class KeyBoardUtils {

        /**
         * 打开软键盘
         *
         * @param mEditText
         * @param mContext
         */
        public static void openKeybord(EditText mEditText, Context mContext) {
            InputMethodManager imm = (InputMethodManager) mContext
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                    InputMethodManager.HIDE_IMPLICIT_ONLY);
        }

        /**
         * 关闭软键盘
         *
         */
        public static void closeKeybord(EditText mEditText, Context mContext) {
            InputMethodManager imm = (InputMethodManager) mContext
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
        }

        /**
         * 判断当前软键盘是否打开
         *
         * @param activity
         * @return
         */
        public static boolean isSoftInputShow(Activity activity) {

            // 虚拟键盘隐藏 判断view是否为空
            View view = activity.getWindow().peekDecorView();
            if (view != null) {
                // 隐藏虚拟键盘
                InputMethodManager inputmanger = (InputMethodManager) activity
                        .getSystemService(Activity.INPUT_METHOD_SERVICE);
//       inputmanger.hideSoftInputFromWindow(view.getWindowToken(),0);

                return inputmanger.isActive() && activity.getWindow().getCurrentFocus() != null;
            }
            return false;
        }
}

